package simri.servidor.jdbc;

import java.sql.*;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConexaoBD {
    protected Connection connection;
    protected ResultSet tabela;

    // *************************************************
    public ConexaoBD(){
    }
    
     
    // *************************************************    
    public void conectar(UsuarioBD usuario) throws Exception{
    	try {
			Context ic = new InitialContext();
			DataSource dataSource = (DataSource) ic.lookup("java:jboss/datasources/simri");
			connection = dataSource.getConnection();
	    	connection.setAutoCommit(true);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} catch (NamingException e) {
			throw new RuntimeException(e);
		}
    }
    
    // *************************************************
    public void close()throws Exception{
    	connection.close();
    }
    
    // *************************************************
    public ResultSet execSelect(String sql) throws Exception{
    	Statement s = connection.createStatement();
    	ResultSet result = s.executeQuery(sql);
    	return result;
    }
    
    // *************************************************
    public void execSQL(String sql) throws Exception{
    	Statement s = connection.createStatement();
    	s.execute(sql);
    }
    

	public Connection getConnection() {
		return connection;
	}


	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	
}